package com.os.meituan.controller.order;

import com.github.pagehelper.PageInfo;
import com.os.meituan.common.PubClass;
import com.os.meituan.common.StringClass;
import com.os.meituan.common.XJsonInfo;
import com.os.meituan.mapper.Order.T_ORDER_TotalPriceMapper;
import com.os.meituan.model.dtomodel.order.DTO_ORDER_GetOrder;
import com.os.meituan.model.pomodel.Shop.T_SHOP_Imformation;
import com.os.meituan.repository.order.T_ORDER_GetOrderResponsitory;
import com.os.meituan.repository.Shop.OA_SHOP_InformationResponsitory;
import com.os.meituan.repository.good.T_SHOP_GoodResponsitory;
import com.os.meituan.repository.good.T_SHOP_Good_DetailTypeResponsitory;
import com.os.meituan.repository.order.T_SHOP_ImformationResponsitory;
import com.os.meituan.service.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;

@Api(tags = "OA-EQM-美团")
@RestController
@RequestMapping("/api/oa/fra/ord")
public class OrderController {
    @Resource
    private T_ORDER_GetOrderResponsitory t_order_getOrderResponsitory;
    @Resource
    private OA_SHOP_InformationResponsitory oa_shop_informationResponsitory;
    @Resource
    private T_SHOP_GoodResponsitory t_shop_goodResponsitory;
    @Resource
    private T_SHOP_Good_DetailTypeResponsitory tShopGoodDetailTypeResponsitory;
    @Resource
    private OrderService orderService;
    @Resource
    private T_ORDER_TotalPriceMapper t_order_totalPriceMapper;
    @Resource
    private T_SHOP_ImformationResponsitory t_shop_imformationResponsitory;
    @Resource
    private PubClass pubClass;
    @ApiOperation("统计")
    @PostMapping("/totalPrice")
    public XJsonInfo totalPrice(@RequestBody DTO_ORDER_GetOrder reObj){

        orderService.totalPrice(reObj);
        return new XJsonInfo();
 
     /*  for(int i=0; i<reObj.getDtoOrderCommerinfos().size();i++){
           var poShopGoodInfo=new T_SHOP_Good();
           poShopGoodInfo.setDiscountPrice(reObj.getDtoOrderCommerinfos().get(i).getDiscountPrice());


           poShopGoodInfo.setName(reObj.getDtoOrderCommerinfos().get(i).getCommerName());

           t_shop_goodResponsitory.save(poShopGoodInfo);
       }
       var poShopImformation=new T_SHOP_Imformation();
       poShopImformation.setId(reObj.getDtoOrderShopInfo().getId());
        poShopImformation.setStoreName(reObj.getDtoOrderShopInfo().getStoreName());
        poShopImformation.setStorePhoneNum(reObj.getDtoOrderShopInfo().getStorePhoneNum());
        poShopImformation.setStoreAddress(reObj.getDtoOrderShopInfo().getStoreAddress());
        poShopImformation.setLatitude(reObj.getDtoOrderShopInfo().getLatitude());
        poShopImformation.setLongitude(reObj.getDtoOrderShopInfo().getLongitude());
        oa_shop_informationResponsitory.save(poShopImformation);
        var poOrder= new T_Order();
        if(reObj.getDtoOrderShopInfo().getId() !=null) {
            var shopGood = t_shop_goodResponsitory.findById(reObj.getDtoOrderShopInfo().getId()).orElse(null);
            poOrder.setPrice(reObj.getOrderInfo().getGoodNum() * shopGood.getDiscountPrice());
            poOrder.setAddress(reObj.getOrderInfo().getAddress());
            poOrder.setCreateTime(reObj.getOrderInfo().getCreateTime());
            poOrder.setDeliveryTime(reObj.getOrderInfo().getDeliveryTime());
            poOrder.setSignTime(reObj.getOrderInfo().getSignTime());
            poOrder.setOrderStatus(reObj.getOrderInfo().getOrderStatus());
            poOrder.setShopId(reObj.getOrderInfo().getShopId());
            t_order_getOrderResponsitory.save(poOrder);
        }else {
            return new XJsonInfo().setMsg("ID不能为空");
        }
        return new XJsonInfo();*/





        /*var toInfo = JsonClass.fromJSON(JsonClass.toJSON(reObj), T_Order.class);
        var toInfo1=JsonClass.fromJSON(JsonClass.toJSON(reObj), T_SHOP_Imformation.class);
        toInfo.setCreateTime(new Date());
        toInfo.setAddress("府城中心小");
        toInfo.setShopId("2");
        toInfo1.setStoreName("小红");
        oa_shop_informationResponsitory.save(toInfo1);
        t_order_getOrderResponsitory.save(toInfo);
        return new XJsonInfo();*/
    }
    @ApiOperation("下单")
    @PostMapping("/saveOrder")
    public XJsonInfo saveOrder(@RequestBody  DTO_ORDER_GetOrder reObj){
        orderService.saveOrder(reObj);
        return new XJsonInfo();
    }

    @ApiOperation("统计某个商店，某个时间段的营业额 ")
    @GetMapping("/getTotalPrice")
    public XJsonInfo getTotalPrice(){
      var totalPrices=  t_order_totalPriceMapper.getTotalPrices();
        PageInfo page = new PageInfo(totalPrices);
        return XJsonInfo.success().setPageData(page);
      /*  return XJsonInfo.success().setData(totalPrices);*/
    }

/*    @ApiOperation("获取所有的商店信息")
    @GetMapping("/getList")
    public XJsonInfo getList(){
     var List=    t_shop_imformationResponsitory.findAll();
     return new XJsonInfo().setData(List);
    }*/



    @ApiOperation("选择查询功能")
    @GetMapping("/getList")
    public XJsonInfo getList(T_SHOP_Imformation info){
        var pageable = pubClass.createPagenation();
        Page page=  oa_shop_informationResponsitory.findAll((Specification<T_SHOP_Imformation>) (root, query, cb )->{
            var list = new ArrayList<Predicate>();

            if (StringClass.hasValue(info.getStoreName())) {
                list.add(cb.like(root.get("storeName"), "%" + info.getStoreName() + "%"));
            }if(StringClass.hasValue(info.getStoreType())){
                list.add(cb.like(root.get("storeType"), "%" + info.getStoreType() + "%"));
            }
            query.where(cb.and(list.toArray(new Predicate[0])));
            return null;

        } ,pageable);
        return new XJsonInfo().setPageData(page);
    }





}
